<template>
  <div class="login-contain">
    <van-form>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名', trigger: 'onBlur' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码', trigger: 'onBlur' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="pawRules"
      />
      <div style="margin: 16px">
        <van-button round block type="primary" @click="register"
          >立即注册</van-button
        >
      </div>
    </van-form>
    <div class="text" @click="$router.push('/login')">已有账号，去登录</div>
  </div>
</template>

<script setup>
import { Toast } from "vant";
import { useRouter } from "vue-router";
const $router = useRouter();
import { reqregister } from "@/api";
import { ref } from "vue";

const username = ref("");
const password = ref("");
const repassword = ref("");
const pawRules = ref([
  { required: true, message: "请填确认密码" },
  {
    // 自定义校验规则
    validator: (value) => {
      return value === password.value;
    },
    message: "密码不一致",
    trigger: "onBlur",
  },
]);

const register = async () => {
  let { message } = await reqregister({
    username: username.value,
    password: password.value,
  });
  Toast(message);
  $router.replace("/login");
};
</script>

<style lang="scss" scoped>
.login-contain {
  font-size: 14px;

  .text {
    font-size: 16px;
    color: #b9b5b5;
    text-align: center;
  }
}
</style>
